package cn.edu.zut.cs.controller;


import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.Weighing;
import cn.edu.zut.cs.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author user
 * @since 2021-06-16
 */
@Controller

public class UserController {

    @Resource
    private UserService userService;

    //修改管理员密码
    @PostMapping("/modifyUser")
    public String modify(HttpSession session, @RequestParam("userpassword")String userpassword, @RequestParam("reuserpassword1")String reuserpassword1, @RequestParam("reuserpassword2")String reuserpassword2, Model model){
        User user = (User) session.getAttribute("loginUser");
        User user1 = userService.selectUserById(user.getUserid());
        if(userpassword.equals(user1.getUserpassword())){
            if(reuserpassword1.equals(reuserpassword2)){
                if(reuserpassword1.isEmpty()){
                    model.addAttribute("msg","密码不能为空");
                }else{
                    user1.setUserpassword(reuserpassword2);
                    model.addAttribute("msg","密码修改成功");
                    userService.updateUserById(user1);
                }

               // session.removeAttribute("loginUser");
            }else{
                model.addAttribute("msg","新密码和确认新密码输入不一致，密码修改失败");
            }
        }else{
            model.addAttribute("msg","旧密码输入错误");
        }
        return "my_modify";
    }

    //查询员工
    @RequestMapping("/searchstaff")
    public String searchstaff(HttpSession session,@RequestParam(value = "location",defaultValue = "-1")String location,@RequestParam(value = "userid",defaultValue = "-1")String userid, @RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model){

        User loginUser = (User) session.getAttribute("loginUser");
        Page<User> Tpage = new Page<>(pn,10);
        if(loginUser.getUserlevel()==1){
            if(location.equals("-1")&&userid.equals("-1")){
                QueryWrapper<User> wrapper=new QueryWrapper<>();
                wrapper.eq("usercompany",loginUser.getUsercompany()).eq("userlevel",2);
                Page<User> page = userService.page(Tpage, wrapper);
                model.addAttribute("users",page);
                model.addAttribute("location",location);
                model.addAttribute("userid",userid);
                return "staff_management";
            }else if(!location.equals("-1")&&userid.equals("-1")){
                QueryWrapper<User> wrapper=new QueryWrapper<>();
                wrapper.eq("usercompany",loginUser.getUsercompany()).eq("userlevel",2).eq("userlocation",location);
                Page<User> page = userService.page(Tpage, wrapper);
                model.addAttribute("users",page);
                model.addAttribute("location",location);
                model.addAttribute("userid",userid);
                return "staff_management1";
            }else if(!userid.equals("-1")&&location.equals("-1")){
                QueryWrapper<User> wrapper=new QueryWrapper<>();
                wrapper.eq("usercompany",loginUser.getUsercompany()).eq("userlevel",2).eq("userid",userid);
                Page<User> page = userService.page(Tpage, wrapper);
                model.addAttribute("users",page);
                model.addAttribute("location",location);
                model.addAttribute("userid",userid);
                return "staff_management1";
            }else{
                QueryWrapper<User> wrapper=new QueryWrapper<>();
                wrapper.eq("usercompany",loginUser.getUsercompany()).eq("userlevel",2).eq("userid",userid).eq("userlocation",location);
                Page<User> page = userService.page(Tpage, wrapper);
                model.addAttribute("users",page);
                model.addAttribute("location",location);
                model.addAttribute("userid",userid);
                return "staff_management1";
            }
        }else{
            return "404";
        }

    }


}

